package com.handu.luna.boss.controller.support;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.InitBinder;

import com.handu.luna.core.domain.BasePersistentDomain;
import com.handu.luna.core.domain.Context;

@Aspect
@Component
public class DomainPropertyAspector {
	
	@Autowired
	Context context;
	
	@SuppressWarnings("rawtypes")
	@Before("execution(* com.handu.luna.boss.controller.*.*(..))")  
    public void processTx(JoinPoint joinPoint) throws java.lang.Throwable {  
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        Method m = ms.getMethod();
        InitBinder binder = m.getAnnotation(InitBinder.class);

        if(binder != null && binder.value()!= null && binder.value().length>0){
            Object[] arguments =  joinPoint.getArgs();
            for(int index = 0;index<arguments.length;index ++){
            	Object arg = arguments[index];
            	if(arg != null && arg instanceof BasePersistentDomain){
    				BasePersistentDomain domain = (BasePersistentDomain)arg;
    				domain.merge(binder.value()); 
            	}
            }
        }
    }  
}
